// page/details/pages/gifBox/gifBox.js
const app = getApp();
var wxbarcode = require("../../../../utils/index.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketList: [],
    noData: true,
    isEnd: false,
    page: 1,
    qrType: false
  },
  reqData() {
    const that = this;
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
      page: 1,
      limit: 5
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getmyGiftBoxStr, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.status == 1) {
        res.result.paginate.data.forEach(item => {
          if (item.hx_status == 1) {
            item.statusMsg = '已使用'
          } else if (item.hx_status == 0) {
            item.statusMsg = '待核销'
          } else if (item.hx_status == 2) {
            item.statusMsg = '已过期'
          }
          if(item.use_start_time == 0 && item.use_end_time == 0){
            item.use_start_time = '长期有效';
          }else{
            item.use_start_time =  app.moment(item.use_start_time * 1000).format('YYYY-MM-DD HH:mm:ss') + '  至  ' + app.moment(item.use_end_time * 1000).format('YYYY-MM-DD HH:mm:ss');
          }
        })
        that.setData({
          ticketList: res.result.paginate.data
        })
        if (that.data.ticketList == '') {
          that.setData({
            noData: false
          })
        } else {
          that.setData({
            noData: true
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },

  //显示二维码
  showQr(e) {
    const that = this;
    that.setData({
      tabCodeIndex: 1,
      qrcode: e.currentTarget.dataset.hx_code + '|'  + e.currentTarget.dataset.encry,
      qrType: true
    })
    setTimeout(() => {
      wxbarcode.qrcode("qrcode", that.data.qrcode, {
        codeSize: 420,
        color: "#222222",
        bgcolor: "#ffffff",
      });
    }, 200)
  },
  remQr() {
    const that = this;
    that.setData({
      qrType: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.reqData();
  },
  onShow: function () {
    //数据统计
    // app.statistics("page/detail/pages/myTicket/myTicket", "优惠券")
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      page: 1,
      isEnd: false
    })
    wx.showNavigationBarLoading()
    that.reqData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    if (that.data.isEnd) {
      return
    }
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
      page: that.data.page + 1,
      limit: 5
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getmyGiftBoxStr, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result.paginate.data.length != 0) {
          res.result.paginate.data.forEach(item => {
            if (item.hx_status == 1) {
              item.statusMsg = '已使用'
            } else if (item.hx_status == 0) {
              item.statusMsg = '待核销'
            } else if (item.hx_status == 2) {
              item.statusMsg = '已过期'
            }
            if(item.use_start_time == 0 && item.use_end_time == 0){
              item.use_start_time = '长期有效';
            }else{
              item.use_start_time =  app.moment(item.use_start_time * 1000).format('YYYY-MM-DD HH:mm:ss') + '  至  ' + app.moment(item.use_end_time * 1000).format('YYYY-MM-DD HH:mm:ss');
            }
          })
          that.setData({
            ticketList: [...that.data.ticketList, ...res.result.paginate.data],
            page: that.data.page + 1
          })
        } else {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: true
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },
})